$(function () {
    setSelectData();
    getUserInfo();
});

function getUserInfo() {
    let username = sessionStorage.getItem("username");
    let data = myAjax("/user/getUserByUsername", "post", {"username": username}, "json");
    console.log(data);
    let user = data.model["user"];
    if (user.name != null)
        $("#name").val(user.name);

    if (user.username != null)
        $("#username").val(user.username);

    if (user.password != null)
        $("#password").val(user.password);

    if (user.phone != null)
        $("#phone").val(user.phone);

    if (user.emergencyPhone != null)
        $("#emergency_phone").val(user.emergencyPhone);

    if (user.gender != null)
        $("input[name=gender][value=" + user.gender.code + "]").attr("checked", true);

    if (user.age != null)
        $("#age").val(user.age);

    if (user.team != null)
        $("#team").find("option[value=" + user.team.code + "]").attr("selected", true);
    else
        $("#team").append("<option value='noteam' selected='selected'>暂未分组</option>");

    if (user.role != null)
        $("#role").find("option[value=" + user.role.code + "]").attr("selected", true);

    if (user.degree != null)
        $("#degree").find("option[value=" + user.degree.code + "]").attr("selected", true);

    if (user.graduate != null)
        $("#graduate").val(user.graduate);

    if (user.nativePlace != null)
        $("#native").val(user.nativePlace);

    if (user.address != null)
        $("#address").val(user.address);

    if (user.status != null)
        $("input[name=status][value=" + user.status.code + "]").attr("checked", true);

}

function userEdit() {
    let name = $("#name").val();
    let username = $("#username").val();
    let password = $("#password").val()
    let cfmpassword = $("#confirmPassword").val();
    let phone = $("#phone").val();
    let emergency_phone = $("#emergency_phone").val();
    let gender = $("input[name=gender]:checked").val();
    let age = $("#age").val();
    let team = $("#team").find("option:selected").val();
    let role = $("#role").find("option:selected").val();
    let degree = $("#degree").find("option:selected").val();
    let graduate = $("#graduate").val();
    let native = $("#native").val();
    let address = $("#address").val();
    let status = $("input[name=status]:checked").val();
    if (cfmpassword==null || ($("#password").val() != $("#confirmPassword").val()))
        password ==null;
    if (team!=null && team!='noteam')
        myAjax("/team/freshTeamCrew", "post", {"code": team}, "json");
    let data = {
        "name": name,
        "username": username,
        "password": password,
        "phone": phone,
        "emergencyPhone": emergency_phone,
        "gender": gender,
        "age": age,
        "team": team,
        "role": role,
        "degree": degree,
        "graduate": graduate,
        "nativePlace": native,
        "address": address,
        "status": status
    };
    let result = myAjax("/user/editUser", "post", data, "json");
    console.log(result);
    if (result==200){
        alert("信息修改成功！");
        let data = myAjax("/user/getUserByUsername","post",{"username":username},"json");
        let user = data.model["user"];
        sessionStorage.setItem("user",JSON.stringify(user));
    }
    else if(result==500)
        alert("修改失败");
    else
        alert("后台错误");
    window.location.href="/html/home.html";
}

function setSelectData() {
    if (sessionStorage.getItem("gender") == null)
        getData();
    let gender = JSON.parse(sessionStorage.getItem("gender"));
    console.log(gender);
    $("#gender").html("");
    $.each(gender, function (i, option) {
        $("#gender").append("<label><input type='radio' name='gender' value=" + option.code + ">" + option.name + "</label>");
    });
    let degree = JSON.parse(sessionStorage.getItem("degree"));
    console.log(degree);
    $("#degree").html("");
    $.each(degree, function (i, option) {
        $("#degree").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
    let status = JSON.parse(sessionStorage.getItem("status"));
    console.log(status);
    $("#status").html("");
    $.each(status, function (i, option) {
        $("#status").append("<label><input type='radio' name='status' value=" + option.code + ">" + option.name + "</label>");
    });
    let role = JSON.parse(sessionStorage.getItem("role"));
    $("#role").html("");
    $.each(role, function (i, option) {
        $("#role").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
    let team = JSON.parse(sessionStorage.getItem("team"));
    console.log(team);
    $("#team").html("");
    $.each(team, function (i, option) {
        $("#team").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
}
function confirmPassword() {
    if ($("#confirmPassword").val() == "") {
        $("#psd_cfm_tips").html("确认密码不能为空");
        return  false;
    } else {
        if ($("#password").val() != $("#confirmPassword").val()) {
            $("#psd_cfm_tips").html("两次密码输入不一致，请重新输入!");
            return  false;
        } else{
            $("#psd_cfm_tips").html("");
            return true;
        }
    }
}